package com.beiming.libra.service.impl;

import com.beiming.libra.common.enums.StatusEnum;
import com.beiming.libra.common.utils.BeanConvertUtils;
import com.beiming.libra.common.utils.CommonsUtils;
import com.beiming.libra.dao.ArbitrationCaseProblemMapper;
import com.beiming.libra.domain.ArbitrationCaseAnswer;
import com.beiming.libra.domain.ArbitrationCaseProblem;
import com.beiming.libra.domain.dto.requestdto.ProblemRequestDTO;
import com.beiming.libra.domain.dto.responsedto.AnswerResponseDTO;
import com.beiming.libra.domain.dto.responsedto.CaseProblemAndAnswerResponseDTO;
import com.beiming.libra.service.ArbitrationCaseAnswerService;
import com.beiming.libra.service.ArbitrationCaseProblemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhengyu
 * @title
 * @create_time 2018/6/6
 */
@Service
public class ArbitrationCaseProblemServiceImpl implements ArbitrationCaseProblemService {

  @Autowired
  private ArbitrationCaseProblemMapper arbitrationCaseProblemMapper;
  @Autowired
  private ArbitrationCaseAnswerService arbitrationCaseAnswerService;

  /**
   * 问题单获取服务
   *
   * @param caseId 案件申请书id
   */
  @Override
  public List<CaseProblemAndAnswerResponseDTO> listArbitrationCaseProblems(
      String caseId) {
    ArbitrationCaseProblem param  = new ArbitrationCaseProblem();
    param.setCaseId(caseId);
    param.setStatus(StatusEnum.USED.getCode());
    List<ArbitrationCaseProblem> arbitrationCaseProblems = arbitrationCaseProblemMapper.select(param);

    //拼装答案
    List<CaseProblemAndAnswerResponseDTO> result = new ArrayList<CaseProblemAndAnswerResponseDTO>();
    for (ArbitrationCaseProblem arbitrationCaseProblem : arbitrationCaseProblems) {
      List<ArbitrationCaseAnswer> arbitrationCaseAnswers = arbitrationCaseAnswerService.listArbitrationCaseAnswers(arbitrationCaseProblem.getId(), null);
      List<AnswerResponseDTO> answerResponseDTOs = BeanConvertUtils.copyList(arbitrationCaseAnswers, AnswerResponseDTO.class);
      result.add(
          CaseProblemAndAnswerResponseDTO.getData(arbitrationCaseProblem, answerResponseDTOs));
    }

    return result;
  }

  /**
   * 问题单精确获取服务
   *
   * @param id id
   */
  @Override
  public CaseProblemAndAnswerResponseDTO getArbitrationCaseProblem(
      String id) {
    ArbitrationCaseProblem arbitrationCaseProblem = arbitrationCaseProblemMapper.selectByPrimaryKey(id);
    //拼装答案信息
    List<ArbitrationCaseAnswer> arbitrationCaseAnswers =  arbitrationCaseAnswerService.listArbitrationCaseAnswers(arbitrationCaseProblem.getId(), null);
    List<AnswerResponseDTO> answerResponseDTOs = BeanConvertUtils.copyList(arbitrationCaseAnswers, AnswerResponseDTO.class);
    return CaseProblemAndAnswerResponseDTO.getData(arbitrationCaseProblem, answerResponseDTOs);
  }

  /**
   * 保存问题单
   *
   * @param arbitrationCaseProblem 新问题单
   */
  @Override
  public void saveArbitrationCaseProblem(ArbitrationCaseProblem arbitrationCaseProblem) {
    arbitrationCaseProblem.setId(CommonsUtils.get32BitUUID());
    //获取当前用户并写入到问题提问人中
    arbitrationCaseProblem.setStatus(StatusEnum.USED.getCode());
    arbitrationCaseProblemMapper.insert(arbitrationCaseProblem);
  }

  /**
   * 修改问题单
   *
   * @param arbitrationCaseProblem 问题单
   */
  @Override public void updateArbitrationCaseProblem(ArbitrationCaseProblem arbitrationCaseProblem) {
    arbitrationCaseProblemMapper.updateByPrimaryKeySelective(arbitrationCaseProblem);
  }

  /**
   * 批量保存问题单
   *
   * @param problemRequestDTO 问题单数组
   */
  @Override public void saveArbitrationCaseProblems(ProblemRequestDTO problemRequestDTO) {
    if (null == problemRequestDTO || null == problemRequestDTO.getProblemTitles() || problemRequestDTO.getProblemTitles().isEmpty()) {
      return;
    }
    Integer i = arbitrationCaseProblemMapper.getSeqNo(problemRequestDTO.getCaseId());
    if (i == null) {
      i = 0;
    }
    for (String problemTitle : problemRequestDTO.getProblemTitles()) {
      //创建新的问题单
      ArbitrationCaseProblem arbitrationCaseProblem = new ArbitrationCaseProblem();
      arbitrationCaseProblem.setCaseId(problemRequestDTO.getCaseId());
      arbitrationCaseProblem.setProblemTitle(problemTitle);
      arbitrationCaseProblem.setUserId(problemRequestDTO.getUserId());
      i++;
      arbitrationCaseProblem.setSeqNo(i);
      saveArbitrationCaseProblem(arbitrationCaseProblem);
    }
  }
}
